package com.example.XueCuApp.View;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.XueCuApp.Base.BaseActivity;
import com.example.XueCuApp.Base.BaseConfig;
import com.example.XueCuApp.Bean.JJHPointListResultBean;
import com.example.XueCuApp.Bean.JJHaddOrRelieveRequsetBean;
import com.example.XueCuApp.Bean.JJHaddOrRelieveResultBean;
import com.example.XueCuApp.Bean.JJHfindPointRequestBean;
import com.example.XueCuApp.Bean.JJHfindPointResultBean;
import com.example.XueCuApp.Bean.JJHfindTerminalNameRequestBean;
import com.example.XueCuApp.Bean.JJHfindTerminalNameResultBean;
import com.example.XueCuApp.Customer.InputInformation;
import com.example.XueCuApp.R;
import com.example.XueCuApp.RetrufitHelp.BaseSubscriber;
import com.example.XueCuApp.RetrufitHelp.ExceptionHandle;
import com.example.XueCuApp.RetrufitHelp.RetrofitClient;
import com.example.XueCuApp.Utils.GetDeviceId;
import com.example.XueCuApp.Utils.LogUtil;
import com.example.XueCuApp.Utils.SPUtil;
import com.example.XueCuApp.Utils.StringUtil;
import com.example.XueCuApp.Utils.ThreadManager;
import com.example.XueCuApp.Utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;


public class BindingActivity extends BaseActivity {

    private InputInformation pointName;
    private InputInformation locationName;
    private InputInformation meationName;
    private Button binging;
    private OptionsPickerView pvNoLinkOptions, pvNoLinkOptionsecond, pvNoLinkOptionThread;
    private List<JJHPointListResultBean.DataBean> pointList;
    private List<String> pointListString;
    private Long scenicSpotBusinessOrgId;
    private String scenicSpotBusinessOrgName;

    private JJHfindPointRequestBean findLocationBean;
    private List<JJHfindPointResultBean.DataBean> locationList;
    private List<String> locationListString;
    private Long pointId;
    private String pointNa;

    private JJHfindTerminalNameRequestBean meationBean;
    private List<JJHfindTerminalNameResultBean.DataBean> meationList;
    private List<String> meationListString;
    private Long terminalId;
    private String terminalName;

    private JJHaddOrRelieveRequsetBean addBean;
    private String DeviceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    // 点击去绑定操作
    private void setBindingMeation() {
        RetrofitClient.getInstance(BindingActivity.this, BaseUrl, headers).JJHaddOrRelieveTerminalBind(addBean, new BaseSubscriber<JJHaddOrRelieveResultBean>(BindingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(JJHaddOrRelieveResultBean jjHaddOrRelieveResultBean) {
                if (jjHaddOrRelieveResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    //绑定成功 去保存信息
                    SPUtil.put(BindingActivity.this,  BaseConfig.JJH_CHECK_POINT, scenicSpotBusinessOrgId);
                    SPUtil.put(BindingActivity.this,  BaseConfig.JJH_CHECK_POINT_NAME, scenicSpotBusinessOrgName);
                    SPUtil.put(BindingActivity.this,  BaseConfig.JJH_CHECK_LOCATION, pointId);
                    SPUtil.put(BindingActivity.this,  BaseConfig.JJH_CHECK_LOCATION_NAME, pointNa);
                    SPUtil.put(BindingActivity.this,  BaseConfig.JJH_CHECK_MEATION, terminalId);
                    SPUtil.put(BindingActivity.this,  BaseConfig.JJH_CHECK_MEATION_NAME, terminalName);
                    setResult(202);
                    finish();
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHaddOrRelieveResultBean.getMessage());
                }
            }
        });
    }

    private void getMeationList() {
        meationBean.setTerminalType(5);
        meationBean.setPointId(pointId);
        meationBean.setTerminalResignFlag(0L);
        RetrofitClient.getInstance(BindingActivity.this, BaseUrl, headers).JJHfindTerminalNameAndIdList(meationBean, new BaseSubscriber<JJHfindTerminalNameResultBean>(BindingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(JJHfindTerminalNameResultBean jjHfindTerminalNameResultBean) {
                if (jjHfindTerminalNameResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    meationList.clear();
                    meationListString.clear();
                    for (int i = 0; i < jjHfindTerminalNameResultBean.getData().size(); i++) {
                        LogUtil.d(jjHfindTerminalNameResultBean.getData().get(i).getTerminalShortName());
                        meationList.add(jjHfindTerminalNameResultBean.getData().get(i));
                        meationListString.add(jjHfindTerminalNameResultBean.getData().get(i).getTerminalShortName() + "  " + jjHfindTerminalNameResultBean.getData().get(i).getTerminalId());
                    }

                    if (meationListString.size() > 0) {
                        pvNoLinkOptionThread.show();
                    } else {
                        ToastUtil.showToast("没有可用点位");
                    }
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHfindTerminalNameResultBean.getMessage());
                }
            }
        });
    }

    private void getLocationList() {
        findLocationBean.setScenicSpotBusinessOrgId(scenicSpotBusinessOrgId);
        RetrofitClient.getInstance(BindingActivity.this, BaseUrl, headers).JJHfindPointSelect(findLocationBean, new BaseSubscriber<JJHfindPointResultBean>(BindingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(JJHfindPointResultBean jjHfindPointResultBean) {
                if (jjHfindPointResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    locationList.clear();
                    locationListString.clear();
                    for (int i = 0; i < jjHfindPointResultBean.getData().size(); i++) {
                        LogUtil.d(jjHfindPointResultBean.getData().get(i).getPointShortName());
                        locationList.add(jjHfindPointResultBean.getData().get(i));
                        locationListString.add(jjHfindPointResultBean.getData().get(i).getPointShortName() + "  " + jjHfindPointResultBean.getData().get(i).getPointId());
                    }

                    if (locationListString.size() > 0) {
                        pvNoLinkOptionsecond.show();
                    } else {
                        ToastUtil.showToast("没有可用点位");
                    }
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHfindPointResultBean.getMessage());
                }
            }
        });
    }

    private void getPointList() {
        RetrofitClient.getInstance(BindingActivity.this, BaseUrl, headers).JJHgetScenicSpotBusinessList(new BaseSubscriber<JJHPointListResultBean>(BindingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(JJHPointListResultBean jjhPointListResultBean) {
                if (jjhPointListResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    pointList.clear();
                    pointListString.clear();
                    for (int i = 0; i < jjhPointListResultBean.getData().size(); i++) {
                        LogUtil.d(jjhPointListResultBean.getData().get(i).getScenicSpotBusinessOrgShortName());
                        pointList.add(jjhPointListResultBean.getData().get(i));
                        pointListString.add(jjhPointListResultBean.getData().get(i).getScenicSpotBusinessOrgShortName() + "  " + jjhPointListResultBean.getData().get(i).getScenicSpotBusinessOrgId());
                    }
                    if (pointListString.size() > 0) {
                        pvNoLinkOptions.show();
                    } else {
                        ToastUtil.showToast("没有可用景点");
                    }
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjhPointListResultBean.getMessage());
                }
            }
        });
    }


    @Override
    protected void initView() {

        if (pointList == null) {
            pointList = new ArrayList<>();
        }

        if (pointListString == null) {
            pointListString = new ArrayList<>();
        }

        if (locationList == null) {
            locationList = new ArrayList<>();
        }

        if (locationListString == null) {
            locationListString = new ArrayList<>();
        }

        if (meationList == null) {
            meationList = new ArrayList<>();
        }

        if (meationListString == null) {
            meationListString = new ArrayList<>();
        }

        if (findLocationBean == null) {
            findLocationBean = new JJHfindPointRequestBean();
        }

        if (meationBean == null) {
            meationBean = new JJHfindTerminalNameRequestBean();
        }

        if (addBean == null) {
            addBean = new JJHaddOrRelieveRequsetBean();
        }

        pointName = (InputInformation) findViewById(R.id.if_point_name);
        locationName = (InputInformation) findViewById(R.id.if_location_name);
        meationName = (InputInformation) findViewById(R.id.if_meation_name);
        binging = (Button) findViewById(R.id.bt_binding);

        Long pName = (Long) SPUtil.get(BindingActivity.this,  BaseConfig.JJH_CHECK_POINT, 0L);
        String pNameStr = (String) SPUtil.get(BindingActivity.this,  BaseConfig.JJH_CHECK_POINT_NAME, "");
        Long lName = (Long) SPUtil.get(BindingActivity.this,  BaseConfig.JJH_CHECK_LOCATION, 0L);
        String lNameStr = (String) SPUtil.get(BindingActivity.this,  BaseConfig.JJH_CHECK_LOCATION_NAME, "");
        Long mName = (Long) SPUtil.get(BindingActivity.this, BaseConfig.JJH_CHECK_MEATION, 0L);
        String mNameStr = (String) SPUtil.get(BindingActivity.this,  BaseConfig.JJH_CHECK_MEATION_NAME, "");

        pointName.setEditName(pNameStr);
        locationName.setEditName(lNameStr);
        meationName.setEditName(mNameStr);

        scenicSpotBusinessOrgId = pName;
        pointId = lName;
        terminalId = mName;
        initNoLinkOptionsPicker();
        initNoLinkOptionsPickerSecond();
        initNoLinkOptionsPickerThread();
    }

    @Override
    protected void initData() {
        //获取设备唯一标识
        ThreadManager.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    DeviceId = GetDeviceId.getDeviceId(BindingActivity.this);
                    //获取保存在sd中的 设备唯一标识符
                    String readDeviceID = GetDeviceId.readDeviceID(BindingActivity.this);
                    //获取缓存在  sharepreference 里面的 设备唯一标识
                    String string = (String) SPUtil.get(BindingActivity.this, BaseConfig.Device_id, "");
                    //判断 app 内部是否已经缓存,  若已经缓存则使用app 缓存的 设备id
                    if (string != null && !string.equals("")) {
                        // 说明有保存，那么久将其放出去呗
                        if (readDeviceID.equals(string)) {
                            //缓存和文件地址一样，直接返回出去
                            DeviceId = readDeviceID;
                        }
                    } else {
                        DeviceId = GetDeviceId.getDeviceId(BindingActivity.this);
                        GetDeviceId.saveDeviceID(DeviceId, BindingActivity.this);
                    }

                    //最后再次更新app 的缓存
                    SPUtil.put(BindingActivity.this, BaseConfig.Device_id, DeviceId);
                    addBean.setTerminalComputerCpuId(DeviceId);

                    System.out.println("zyf :" + DeviceId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            addBean.setTerminalComputerCpuId(DeviceId);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        binging.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去绑定
                iosLoadDialog.show();
                if (StringUtil.isNullOrEmpty(terminalId + "")) {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast("未选择终端名称");
                    return;
                }
                addBean.setTerminalId(terminalId + "");
                addBean.setTerminalSetFlag(0 + "");
                addBean.setTerminalComputerName("手持机");
                setBindingMeation();
            }
        });


        pointName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取景点名称列表
                iosLoadDialog.show();
                getPointList();
            }
        });

        locationName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取
                iosLoadDialog.show();
                if (StringUtil.isNullOrEmpty(String.valueOf(scenicSpotBusinessOrgId))) {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast("请先选择景区名称");
                    return;
                }
                getLocationList();
            }
        });


        meationName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iosLoadDialog.show();
                if (StringUtil.isNullOrEmpty(String.valueOf(pointId))) {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast("请先选择景点名称");
                    return;
                }
                getMeationList();
            }
        });

    }

    @Override
    public int initLayoutId() {
        return R.layout.activity_binding;
    }


    // 不联动的多级选项
    private void initNoLinkOptionsPicker() {
        pvNoLinkOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                pointName.setEditName(pointList.get(options1).getScenicSpotBusinessOrgShortName() + "");
                scenicSpotBusinessOrgId = pointList.get(options1).getScenicSpotBusinessOrgId();
                scenicSpotBusinessOrgName = pointList.get(options1).getScenicSpotBusinessOrgShortName();
            }
        })
                .setItemVisibleCount(10)
//                 .setSelectOptions(0, 1, 1)
                .build();
        pvNoLinkOptions.setNPicker(pointListString, null, null);
        pvNoLinkOptions.setSelectOptions(0, 1, 1);
    }


    private void initNoLinkOptionsPickerSecond() {
        pvNoLinkOptionsecond = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                locationName.setEditName(locationList.get(options1).getPointShortName() + "");
                pointId = locationList.get(options1).getPointId();
                pointNa = locationList.get(options1).getPointShortName();
            }
        })
                .setItemVisibleCount(10)
//                 .setSelectOptions(0, 1, 1)
                .build();
        pvNoLinkOptionsecond.setNPicker(locationListString, null, null);
        pvNoLinkOptionsecond.setSelectOptions(0, 1, 1);
    }


    private void initNoLinkOptionsPickerThread() {
        pvNoLinkOptionThread = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                meationName.setEditName(meationList.get(options1).getTerminalShortName() + "");
                terminalId = meationList.get(options1).getTerminalId();
                terminalName = meationList.get(options1).getTerminalShortName();
            }
        })
                .setItemVisibleCount(10)
//                 .setSelectOptions(0, 1, 1)
                .build();
        pvNoLinkOptionThread.setNPicker(meationListString, null, null);
        pvNoLinkOptionThread.setSelectOptions(0, 1, 1);
    }
}
